package kamikaze.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.google.appengine.api.datastore.Key;

@Entity
public class Product implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key key;

	private String category;
	private String name;
	private String picture;
	private BigDecimal price = BigDecimal.ZERO;
	private String comment;
	private Long ownerKey;
	private String ownerName;
	private Long productId;
	private boolean deleted;
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdDate;
	private Integer rating=0;

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getOwnerKey() {
		return ownerKey;
	}

	public void setOwnerKey(Long ownerKey) {
		this.ownerKey = ownerKey;
	}
	
	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public Long getProductId() {
		return productId;
	}

	public void setProductId(Long productId) {
		this.productId = productId;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	public Integer getRating() {
		return (rating==null?0:rating);
	}

	public void setRating(Integer rating) {
		this.rating = rating;
	}

	public List<String> validate() {
		List<String> errorMessages = new ArrayList<String>();

		if (category == null || category.isEmpty()) {
			errorMessages.add("Category cannot be empty");
		} else if (category.length() > 100) {
			errorMessages.add("Category maximum 100 characters");
		}

		if (name == null || name.isEmpty()) {
			errorMessages.add("Name cannot be empty");
		} else if (name.length() > 100) {
			errorMessages.add("Name maximum 100 characters");
		}

		if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
			errorMessages.add("Price cannot be negative");
		}

		return errorMessages;
	}

}
